package cn.blt.utils;


import cn.blt.security.auth.entity.LoginUserDetails;
import cn.blt.security.auth.entity.vo.UserVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * jwt 工具类
 * @author admin
 * @date 2022/08/04
 */
@Slf4j
public class JwtUtils {

    public static final String SUBJECT = "hangmu";

    //秘钥
    public static final String APPSECRET = "hangmu";

    public static final long EXPIRE = 1000 * 60 * 60 * 12; //过期时间，毫秒，30分钟


    /**
     * 生成jwt token
     * @param user 用户
     * @return {@link String}
     */
    public static String geneJsonWebToken(UserVo user) {

        if (user == null || StringUtils.isEmpty(user.getUserAcct()) || StringUtils.isEmpty(user.getId())) {
            return null;
        }

        return Jwts.builder().setSubject(SUBJECT)
                .claim("id",String.valueOf(user.getId()))
                .claim("userAcct", user.getUserAcct())
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                .signWith(SignatureAlgorithm.HS256, APPSECRET).compact();
    }


    /**
     * 校验jwt token
     * @param token 令牌
     * @return {@link Claims}
     */
    public static Claims checkJWT(String token) {
        return Jwts.parser().setSigningKey(APPSECRET).parseClaimsJws(token).getBody();
    }


    /**
     * 生成jwt令牌
     * @param user 用户
     * @return {@link String}
     */
    private static String generateJwtToken(UserVo user){

        user.setId(user.getId());
        user.setUserAcct(user.getUserAcct());

        String token = JwtUtils.geneJsonWebToken(user);
        log.info("加密token => {}",token);

        return token;

    }


    /**
     * 解密Jwt令牌
     * @param token 令牌
     */
    private static void decryptJwtToken(String token){

        Claims claims = JwtUtils.checkJWT(token);

        String id = (String) claims.get("id");

        String userAcct = (String)claims.get("userAcct");

        log.info("解密 id => {}",id);

        log.info("解密 userAcct => {}",userAcct);

    }


    /**
     * 请求获取用户
     * @param request 请求
     * @return {@link LoginUserDetails}
     */
    public static LoginUserDetails reqGetUser(HttpServletRequest request){
        String accessToken = request.getHeader("accessToken");

        Claims claims = JwtUtils.checkJWT(accessToken);

        LoginUserDetails userDatils = new LoginUserDetails();

        userDatils.setId((String) claims.get("id"));
        userDatils.setUserAcct((String) claims.get("userAcct"));

        return userDatils;
    }

    /**
     * 得到用户id
     * @param token 令牌
     * @return {@link Long}
     */
    public static Long getUserId (String token) {
        Claims claims = JwtUtils.checkJWT(token);
        String id = (String) claims.get("id");
        return Long.parseLong(id);
    }



}